//
//  ChivoxSysInfo.m
//  ChivoxEngine
//
//  Created by xianchen.peng on 2018/9/27.
//  Copyright © 2018 Chivox. All rights reserved.
//

#import "ChivoxSysInfo.h"
#import "ChivoxSdkVersion.h"
#import <sys/utsname.h>

#if TARGET_OS_IPHONE
#import <UIKit/UIDevice.h>
#define _PLATFORM "ios"
#define _SOURCE 5

#elif TARGET_OS_MAC
#define _PLATFORM "darwin"
#define _SOURCE 3

#else
#error "unknown platform"
#endif

@implementation ChivoxSysInfo

static ChivoxSysInfo *_sysInfo = nil;

+ (ChivoxSysInfo *)getSysInfo {
    char tmp[1024];
    char *p = NULL;
    
    if (_sysInfo == nil) {
        _sysInfo = [[ChivoxSysInfo alloc] init];
        _sysInfo.sdkVersion = CHIVOX_SDK_VERSION;
        _sysInfo.sdkSource = _SOURCE;
        _sysInfo.sdkVersionStr = [ChivoxSysInfo getSdkVersionString];
        _sysInfo.sdkFullVersion = [ChivoxSysInfo getSdkFullVersion];
        
        // get arch
        struct utsname host;
        uname(&host);
        memset(tmp, 0, sizeof(tmp));
        strncpy(tmp, host.machine, sizeof(tmp));
        tmp[sizeof(tmp)-1] = 0;
        if (NULL != (p=strchr(tmp, '\n'))){*p='\0';}
        if (NULL != (p=strchr(tmp, '\r'))){*p='\0';}
        _sysInfo.arch = [NSString stringWithUTF8String:tmp];
        
#if TARGET_OS_IPHONE
        _sysInfo.os = [[UIDevice currentDevice] systemName];
        _sysInfo.osVersion = [[UIDevice currentDevice] systemVersion];
        _sysInfo.productName = [[UIDevice currentDevice] model];
#elif TARGET_OS_MAC
        _sysInfo.os = @"";
        _sysInfo.osVersion = @"";
        _sysInfo.productName = @"";
        
        FILE *file = file = popen("sw_vers", "r");
        if (NULL != file) {
            while(fgets(tmp, sizeof(tmp), file)) {
                char *p = NULL;
                p = strchr(tmp, '\n');
                if (p) *p = '\0';
                p = strchr(tmp, '\r');
                if (p) *p = '\0';
                
                if(0 == strncmp(tmp, "ProductName:", strlen("ProductName:"))) {
                    if (strlen(tmp) > strlen("ProductName:")+1) {
                        _sysInfo.os = [NSString stringWithUTF8String:tmp+strlen("ProductName:")+1];
                    }
                }
                else if(0 == strncmp(tmp, "ProductVersion:", strlen("ProductVersion:"))) {
                    if (strlen(tmp) > strlen("ProductVersion:")+1) {
                        _sysInfo.osVersion = [NSString stringWithUTF8String:tmp+strlen("ProductVersion:")+1];
                    }
                }
            }
        }
#endif
    }
    return _sysInfo;
}

+ (unsigned int)getSdkVersionNumber {
    return CHIVOX_SDK_VERSION;
}

+ (NSString *)getSdkVersionString {
    char ver[64] = {0};
    unsigned int version = CHIVOX_SDK_VERSION;
    unsigned int major = (uint8_t)((version & 0xff000000) >> 24);
    unsigned int minor = (uint8_t)((version & 0x00ff0000) >> 16);
    unsigned int minor2 = (uint8_t)((version & 0x0000ff00) >> 8);
    unsigned int revision = (uint8_t)((version & 0x000000ff));
    snprintf(ver, sizeof(ver), "%d.%d.%d.%d", major, minor, minor2, revision);
    return [NSString stringWithUTF8String:ver];
}

+ (NSString *)getSdkFullVersion {
    NSString *version = [ChivoxSysInfo getSdkVersionString];
    return [NSString stringWithFormat:@"ChivoxEngine-%s-%@", _PLATFORM, version];
}

@end
